<?php
// 购物车控制器
namespace App\Http\Controllers;

// 用户模型
use App\Models\Business\Business as BusinessModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Type as TypeModel;
use App\Models\Product\Cart as CartModel;

// 引入Validator 门面 
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;



class Cart extends Controller
{
    // 添加
    public function add()
    {
        $proid = request('proid', 0); //接收产品id
        $busid = request('busid', 0); //接收用户id

        $productinfo = ProductModel::find($proid); //查询产品信息

        // 判断产品是否存在
        if (!$productinfo) {
            return error('产品不存在');
        }

        $businessinfo = BusinessModel::find($busid); //查询用户信息

        // 判断用户是否存在
        if (!$businessinfo) {
            return error('用户不存在');
        }

        $cartinfo = CartModel::where(['proid' => $proid, 'busid' => $busid])->first(); //查询购物车信息

        // 判断购物车是否存在
        if ($cartinfo) {
            $nums = bcadd($cartinfo['nums'], 1); //购物车数量+1
            // 商品总价
            $total = bcmul($cartinfo['price'], $nums, 2);

            $result = CartModel::where('id', $cartinfo['id'])->update(['nums' => $nums, 'total' => $total]); //更新购物车信息

            // 判断更新是否成功
            if ($result === false) {
                return error('添加失败');
            } else {
                return success('添加成功');
            }
        } else {
            $data = [
                'busid' => $busid,
                'proid' => $proid,
                'nums' => 1,
                'price' => $productinfo['price'],
                'total' => $productinfo['price'],
            ];

            //校验器 验证规则 验证字段 必填
            $rules = [
                'busid' => 'required',
                'proid' => 'required',
                'nums' => 'required',
                'price' => 'required',
                'total' => 'required',
            ];

            // 错误提示信息
            $message = [
                'busid.required' => '用户必填',
                'proid.required' => '商品必填',
                'nums.required' => '数量必填',
                'price.required' => '价格必填',
                'total.required' => '商品总价必填'
            ];

            // make 方法将会生成一个新的验证器实例
            $validator = Validator::make($data, $rules, $message);

            if ($validator->fails()) {
                // first 方法检索给定字段的第一个错误信息
                return error($validator->errors()->first());
            }

            $result = CartModel::create($data);

            if ($result === false) {
                return error('添加失败');
            } else {
                return success('添加成功');
            }
        }
    }

    //列表
    public function index()
    {
        $busid = request('busid', 0);
        $cartids = request('cartids', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        // 判断查询购物车商品，从购物车或者从商品详情立即购买获取
        $dp = CartModel::query();

        if ($cartids != 0) {
            $ids = explode(',', $cartids);
            $dp->whereIn('id', $ids);
        }

        $data = $dp->with('product')->where('busid', $busid)->orderBy('id', 'desc')->get();

        return success('', $data);
    }

    // 编辑
    public function edit()
    {
        $busid = request('busid', 0);
        $nums = request('nums', '');
        $cartid = request('cartid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $cartinfo = CartModel::find($cartid);

        if (!$cartinfo) {
            return error('购物车商品不存在');
        }

        $productinfo = ProductModel::find($cartinfo['proid']);

        if (!$productinfo) {
            return error('商品不存在');
        }

        // 判断商品库存
        $stock = bcsub($productinfo['stock'], $nums);

        if ($stock < 0) {
            return error($productinfo['name'] . '库存不足');
        }

        // 商品总价
        $total = bcmul($cartinfo['price'], $nums, 2);

        $result = CartModel::where('id', $cartinfo['id'])->update(['nums' => $nums, 'total' => $total]);

        if ($result === false) {
            return error('编辑失败');
        } else {
            return success('编辑成功');
        }
    }

    // 删除
    public function del()
    {
        $busid = request('busid', 0);
        $cartid = request('cartid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $cartinfo = CartModel::find($cartid);

        if (!$cartinfo) {
            return error('购物车商品不存在');
        }

        $result = CartModel::destroy($cartid);

        if ($result === false) {
            return error('删除失败');
        } else {
            return success('删除成功');
        }
    }

}
